package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import mode.User;
import util.SocketUtil;
import util.StateCode;

public class InfoPanel extends JPanel implements ActionListener,Observer{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel player;
	private DefaultTableModel dtm;
	private JTextArea jta;
	private JTextField jtf;
	private String time;
	private String name;
	
	public InfoPanel() {
		// TODO Auto-generated constructor stub
	   setBackground(Color.LIGHT_GRAY);
	   setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
	   
	   JPanel top=new JPanel();
	   top.setLayout(new GridBagLayout());
	   GridBagConstraints gbc=new GridBagConstraints();
	   JLabel lable=new JLabel();
	   lable.setIcon(new ImageIcon(getClass().getResource("../image/2.jpg")));
	   gbc.gridx=0;
	   gbc.gridy=0;
	   gbc.insets=new Insets(20, 0, 10, 0);
	   top.add(lable, gbc);
	   player=new JLabel("玩家1");
	   gbc.gridy=1;
	   top.add(player, gbc);
	   add(top);
	   
	   JPanel middle=new JPanel();
	   middle.setLayout(new BorderLayout());
	   dtm=new DefaultTableModel(new Object[][]{}, new String[]{"昵称","IP","联机时间"});
	   JTable table=new JTable(dtm);
	   table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
	   JScrollPane sp=new JScrollPane(table);
	   sp.setPreferredSize(new Dimension(230, 200));
	   middle.add(sp);
	   add(middle);
	   
	   JPanel bottom=new JPanel();
	   bottom.setLayout(new BorderLayout());
	   jta=new JTextArea();
	   jta.setPreferredSize(new Dimension(200, 400));
	   jta.setEditable(false);
       jta.setDisabledTextColor(Color.BLACK);
       jta.setCaretPosition(jta.getText().length());
       jta.setBorder(BorderFactory.createLoweredBevelBorder());
       JScrollPane js=new JScrollPane(jta);
       bottom.add(js);
       JPanel panel=new JPanel();
       panel.setLayout(new BorderLayout());
       jtf=new JTextField(10);
       panel.add(jtf);
       JButton but=new JButton("发送");
       but.addActionListener(this);
       panel.add(but,BorderLayout.EAST);
       bottom.add(panel,BorderLayout.SOUTH);
       add(bottom);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String message=jtf.getText();
		jta.append("我：(" + time + ")"+ "\n" + message + "\n");
		SocketUtil.sendMessage(message);
		jtf.setText("");
	}

	@Override  
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		if(arg instanceof User) {
			User user=(User) arg;
			dtm.addRow(new Object[]{user.getName(),user.getIp(),user.getLogintime()});
			if(o.equals(user)) {player.setText(user.getName());time=user.getLogintime();name=user.getName();}
		}else if(arg instanceof String){
			String message=(String) arg;
			jta.append(SocketUtil.getUser().getName() + "(" + time + ")" + "\n" + message + "\n");
		}else if(arg instanceof Integer){
			switch ((int)arg) {
			case StateCode.LINKRESET:
				for(int i=0;i<dtm.getRowCount();i++) dtm.removeRow(i);
				break;
			default:
				break;
			}
		}
	}
}
